<?php
/**
 * Created by PhpStorm.
 * Author: Shadow
 * Date: 2022/1/16
 * Time: 9:30 下午
 * description: Qiniu.php
 */


namespace app\common;

use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use think\facade\Env;

class Qiniu
{
    public static function upload($filePath, $filename)
    {
        $filename = str_replace('\\', '/', $filename);//替换\斜杠
        // 需要填写你的 Access Key 和 Secret Key
        $accessKey = 'z6BH7AEcZltkZlff9E3Qtv7-LaRxZVnpjtCxo78H';
        $secretKey = 'CqkG-Lj4AD6TaNbh8qLqtuyDR41U1tX7yifQsMa-';
        $bucket = '1910';
        $domain = 'http://r5slxf5d7.hn-bkt.clouddn.com';
        // 构建鉴权对象
        $auth = new Auth($accessKey, $secretKey);
        // 生成上传 Token
        $token = $auth->uploadToken($bucket);
        // 初始化 UploadManager 对象并进行文件的上传。
        $uploadMgr = new UploadManager();
        // 调用 UploadManager 的 putFile 方法进行文件的上传。
        $res = $uploadMgr->putFile($token, $filename, $filePath);
        if ($res[1] == null) { //上传成功
            return $domain . $filename;
        } else {//上传失败
            return '';
        }
    }
}